<?php
//建立数据库连接
require '../script/mysqlconfig.php';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}

//判断是否传递brand参数，存在则继续后面的插入操作
if (isset($_POST['brand'])) {
    $value1 = trim($_POST["brand"]);
    //$brand不能为空值，品牌字段要排除空值
    if (empty($value1)) {
        echo "字段不能为空，添加失败！" . "<br/>";
    } else {
        $sql = "INSERT INTO brand (name) VALUES ('$value1')";

        if ($conn->query($sql) === true) {
            echo "字段添加成功。";
        } else {
            echo "字段添加失败！" . "<br/>" . $conn->error;
        }
    }
} else {
    //POST没有传递brand参数，表示只是查询
    $sql = "SELECT * FROM brand";
    $result = $conn->query($sql);
// 生成表格
    if ($result->num_rows > 0) {
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>设备品牌</th></tr>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["name"]."</td></tr>";
        }
        echo "</table>";
    } else {
        echo "数据库中没有数据";
    }
}
//关闭数据库连接
$conn->close();
